package com.dingceng.demo.photo

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.dingceng.demo.base.ui.BaseFragment
import com.dingceng.demo.photo.databinding.FragmentTestBinding
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class TestFragment : BaseFragment<FragmentTestBinding>() {


    private lateinit var viewModel: TestViewModel

    companion object {
        @JvmStatic
        fun newInstance() =
            TestFragment().apply {
                arguments = Bundle().apply {
                }
            }
    }

    override fun provideLayout(): Int {
       return R.layout.fragment_test
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel = ViewModelProvider(this).get(TestViewModel::class.java)
        test()
    }

    private fun test() {
        /**Fragment中启动协程。已经绑定了生命周期，会随Fragment销毁自动停止任务，避免了内存协程的隐患*/
        lifecycleScope.launch { //延时2s更新UI
            delay(2000)/**kotlin协程提供的延时方法*/
            binding.tv.text = "666"
        }
    }
}